package com.hooker.common;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Executor;

/**
 * @author lenovo
 * @date 2020/7/14
 */
public class HookHelper {
    private static Executor UiExecutor = new MainThreadExecutor();

    private static class MainThreadExecutor implements Executor {
        private Handler mainThreadHandler = new Handler(Looper.getMainLooper());

        @Override
        public void execute(Runnable command) {
            mainThreadHandler.post(new InnerRunnable(command));
        }
    }

    public static void submitUiThread(Runnable runnable) {
        try {
            UiExecutor.execute(new InnerRunnable(runnable));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private static class InnerRunnable implements Runnable {
        private Runnable runnable;

        public InnerRunnable(Runnable runnable) {
            this.runnable = runnable;
        }

        @Override
        public void run() {
            try {
                if (runnable != null) {
                    runnable.run();
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

}
